"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Job_1 = require("../job/Job");
const JobStatus_1 = require("../job/JobStatus");
const Queue_1 = require("../queue/Queue");
const index_1 = require("./index");
class QueueManager {
    constructor() {
        this.queueInfos = {};
        this.successJobsSize = 0;
        this.failJobsSize = 0;
        this.running = 0;
        this.pause = false;
    }
    addOnStartConfig(config) {
        let queueName = this.addQueueConfig(null, config);
        let url = config.url.constructor === Array ? config.url : [config.url];
        this.addToQueue({
            queueName,
            urls: url
        });
    }
    addQueueConfig(queueName, config) {
        queueName = queueName ? queueName : config['type'] + '_' + config['target'].constructor.name + '_' + config['method'];
        if (config.running == null) {
            config.running = true;
        }
        if (config.parallel == null) {
            config.parallel = 3; // TODO
        }
        let queueInfo = this.queueInfos[queueName];
        if (!queueInfo) {
            this.queueInfos[queueName] = queueInfo = {
                queue: new Queue_1.default()
            };
        }
        queueInfo.queueConfig = config;
        // TODO
        queueInfo.curParallel = 0;
        queueInfo.maxParallel = config.parallel;
        queueInfo.successJobsSize = 0;
        queueInfo.failJobsSize = 0;
        return queueName;
    }
    addToQueue(config) {
        let queueInfo = this.queueInfos[config.queueName];
        let queue = queueInfo.queue;
        config.urls.forEach(url => {
            let job = new Job_1.default(url);
            job.setQueueName(config.queueName);
            job.setTryNum(3);
            job.setStatus(JobStatus_1.default.Created);
            queue.push(job);
            index_1.jobManager.save(job);
        });
    }
}
exports.default = QueueManager;
